#include "Explosion.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <iostream>

using namespace std;

Explosion::Explosion(double x, double y, double angle, double vitesse)
{
    this->x = x;
    this->y = y;
    this->angle = angle;
    this->vitesse = vitesse;

    int nbExplosion = rand()%7+5;
    this->lstExplosion = vector<point*>();

    for(int i=0;i<nbExplosion;i++) {
        point* p = new point(i*((rand()%20)-10)/nbExplosion, (i%3)*((rand()%20)-10));
        this->lstExplosion.push_back(p);
    }

    startTime = SDL_GetTicks();
}

Explosion::~Explosion()
{
    lstExplosion.clear();
}


void Explosion::Draw() {
    glPushMatrix();
    glTranslated(x, y, 0);

    for(int i=0; i<this->lstExplosion.size(); i++) {
        glPushMatrix();

        point* p = lstExplosion.at(i);
        glTranslated(p->x, p->y, 0);

        int r = rand()%255;
        int b = rand()%255;
        int v = rand()%255;

        glColor3ub(r, b, v);
        double nb_faces = 16;
        double rayon = 10;
        double angle;
        glBegin(GL_POLYGON);
            for(int i=0; i<nb_faces; i++)
            {
                angle = 2*M_PI*i/nb_faces;
                glVertex2d( cos(angle)*rayon, sin(angle)*rayon );
            }
        glEnd();
        glPopMatrix();
    }
    glPopMatrix();
}

bool Explosion::isTerminated() {
    Uint32 currTime = SDL_GetTicks();
    const Uint32 delay = 2000;
    if(currTime < (delay+startTime)) {
        return false;
    }
    return true;
}

void Explosion::Deplacer(){
    this->x += cos(angle*M_PI/180)*vitesse/15;
    this->y += sin(angle*M_PI/180)*vitesse/15;
}
